package oppsa.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Toolkit;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowFocusListener;
import java.util.Map.Entry;
import java.util.TreeMap;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import net.miginfocom.swing.MigLayout;
import oppsa.controller.Translate;
import oppsa.controller.User;
import oppsa.model.ActionDB;
import oppsa.model.UserDB;

import org.jdesktop.swingx.JXFrame;

public class AssignToBox extends JXFrame {
	private static final long serialVersionUID = 1L;
	private JComboBox<String> comboBoxResponsible;
	private TreeMap<Integer, String> responsibleMap;
	private HomeView hv;
	private int[] liste;
	private JPanel panel;
	private JLabel lblAddUser;
	private JLabel lblUserChoice;
	private UserDB userList;
	private JButton btnSubmit;
	private ActionDB assignAction;
	private JButton source;
	private JButton btnCancel;

	private Translate t = new Translate();
	
	public AssignToBox(int[] liste, final HomeView hv) {
		addWindowListener(closeWindow);
		setIconImage(Toolkit.getDefaultToolkit().getImage(AssignToBox.class.getResource("/LOGO_FONCE.png")));
		this.liste= new int[liste.length];
	    System.arraycopy(liste, 0, this.liste, 0, liste.length);
		addWindowListener(new WindowAdapter() {
			@Override
			public void windowClosing(WindowEvent arg0) {
				AssignToBox.this.hv.refresh(new JTableModel());
			}
		});
		this.hv = hv;
		
		hv.addWindowFocusListener(clickhomeview);
		setStartPosition(StartPosition.CenterInScreen);
		getRootPaneExt().getContentPane().setBackground(Color.RED);
		setMinimumSize(new Dimension(400, 200));
		setPreferredSize(new Dimension(400, 200));
		setResizable(false);
		
		setTitle(t.getLocalizedText("tlUsersList"));
 
		panel = new JPanel();
		panel.setBackground(Color.WHITE);
		getRootPaneExt().getContentPane().add(panel, BorderLayout.CENTER);
		panel.setLayout(new MigLayout("", "[][grow]", "[][grow][][][][grow]"));

		lblAddUser = new JLabel(t.getLocalizedText("lblUsersList"));
		lblAddUser.setFont(new Font("Tahoma", Font.PLAIN, 20));
		panel.add(lblAddUser, "cell 0 0");

		lblUserChoice = new JLabel(t.getLocalizedText("lblSelectResponsible"));
		lblUserChoice.setFont(new Font("Tahoma", Font.BOLD, 11));
		panel.add(lblUserChoice, "cell 0 2,alignx left");

		responsibleMap = new TreeMap<Integer, String>();
		comboBoxResponsible = new JComboBox<String>();

		userList = new UserDB();
		userList.select(false);
		comboBoxResponsible.addItem("");
		for (User u : userList.getFullList()) {
			comboBoxResponsible.addItem(u.getLastName() + " "
					+ u.getFirstName());
			responsibleMap.put(u.getSequence(),
					u.getLastName() + " " + u.getFirstName());
		}
		comboBoxResponsible.setPreferredSize(new Dimension(110, 22));
		panel.add(comboBoxResponsible, "cell 1 2,alignx left");

		btnSubmit = new JButton("");
		btnSubmit.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
		btnSubmit.setBorder(null);
		btnSubmit.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent arg0) {
				assignAction = new ActionDB();
				for (int seq : AssignToBox.this.liste) {
					assignAction.assignBySeq(seq,getKeyForValue(comboBoxResponsible.getSelectedItem().toString(),responsibleMap));
				}
				setVisible(false);
				AssignToBox.this.hv.refresh(new JTableModel());
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("SubmitS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Submit"))));
			}
		});
		panel.add(btnSubmit, "flowx,cell 1 4");

		btnCancel = new JButton("");
		btnCancel.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
		btnCancel.setBorder(null);
		btnCancel.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseReleased(MouseEvent e) {
		
				hv.setVisible(true);
				hv.setEnabled(true);
				hv.toFront();
			
			}

			@Override
			public void mouseEntered(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("CancelS"))));
			}

			@Override
			public void mouseExited(MouseEvent e) {
				source = (JButton) e.getSource();
				source.setIcon(new ImageIcon(getClass().getResource(t.getLocalizedText("Cancel"))));
			}
		});
		panel.add(btnCancel, "cell 1 4");
	}

	private int getKeyForValue(String value, TreeMap<Integer, String> map) {
		for (Entry<Integer, String> entry : map.entrySet()) {
			if (entry.getValue().equals(value)) {
				return entry.getKey();
			}
		}
		return -1;
	}		
	WindowAdapter closeWindow = new WindowAdapter() {
		@Override
		public void windowClosing(WindowEvent arg0) {
			
			setVisible(false);
			hv.setVisible(true);
			hv.setEnabled(true);
			hv.toFront();

		}
	};
	WindowFocusListener  clickhomeview = new  WindowFocusListener () {

		@Override
		public void windowGainedFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			toFront();
		}

		@Override
		public void windowLostFocus(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}
	};
}
